using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Net.Mail;

namespace atomtask
{
	public partial class GlobalData
	{
		public class User
		{
			public static string user_id;
			public static string username;
			public static string token;
			public static string email;

			// Check username validity
			public static bool checkUsername (string username)
			{
				Regex usernameRgx = new Regex ("^[A-Za-z0-9. _]{4,50}$");
				if (usernameRgx.IsMatch (username))
					return true;
				return false;
			}

			// Check password validity
			public static bool checkPassword (string password)
			{
				Regex passwordRgx = new Regex ("^[A-Za-z0-9. _!@#$%^&*()-]{6,50}$");
				if (passwordRgx.IsMatch (password))
					return true;
				return false;
			}

			// Check e-mail validity
			public static bool checkEmail (string email)
			{
				try
				{
					MailAddress mailAdd = new MailAddress(email);
					return true;
				}
				catch (FormatException)
				{
					return false;
				}
			}

			// Produce the MD5 hash value of the input string
			public static string GetMd5Hash(string input)
			{
				// Create MD5 hash
				MD5 md5Hash = MD5.Create ();

				// Convert the input string into a byte array and compute the hash
				byte[] data = md5Hash.ComputeHash (Encoding.UTF8.GetBytes (input));

				// Dispose the MD5 hash
				md5Hash.Dispose ();

				// Create a new StringBuilder to collect the bytes
				StringBuilder sBuilder = new StringBuilder ();

				// Loop through each byte of the hashed data
				// and format each one as a hexadecimal string
				for (int i = 0; i < data.Length; ++i) {
					sBuilder.Append (data [i].ToString ("x2"));
				}

				// Return the result
				return sBuilder.ToString ();
			}
		}
	}
}
